package cn.yunyichina.provider.pay.service;

import cn.yunyichina.provider.iface.entity.dto.pay.AlipayBillDownloadRequestDTO;
import cn.yunyichina.provider.pay.entity.PaymentSetting;
import cn.yunyichina.provider.pay.exception.PaySettingNotFoundException;
import cn.yunyichina.utils.commons.DateUtils;
import com.alibaba.fastjson.JSON;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lullaby on 2017/7/11.
 */
public class BaseTradeBillService extends BaseService {

    protected AlipayBillDownloadRequestDTO buildAlipayBillDownloadParameters(Map<String, String> params) throws PaySettingNotFoundException {
        String appId = params.get("app_id");
        PaymentSetting paymentSetting = this.getPaymentSetting(appId);
        String signType = "RSA";
        if (paymentSetting.getPayPublicKey().length() > 256) {
            signType = "RSA2";
        }

        AlipayBillDownloadRequestDTO requestDTO = new AlipayBillDownloadRequestDTO();
        requestDTO.setPayPublicKey(paymentSetting.getPayPublicKey());
        requestDTO.setPublicKey(paymentSetting.getPublicKey());
        requestDTO.setPrivateKey(paymentSetting.getPrivateKey());

        requestDTO.setAppId(paymentSetting.getPayAppId());
        requestDTO.setMethod("alipay.data.dataservice.bill.downloadurl.query");
        requestDTO.setFormat("JSON");
        requestDTO.setCharset("utf-8");
        requestDTO.setSignType(signType);
        requestDTO.setTimestamp(DateUtils.dateToString(Calendar.getInstance().getTime()));
        requestDTO.setVersion("1.0");

        Map<String, String> bizContent = new HashMap<>();
        bizContent.put("bill_type", "trade");
        bizContent.put("bill_date", params.get("bill_date"));

        requestDTO.setBizContent(JSON.toJSONString(bizContent));

        return requestDTO;
    }

}
